/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xthread_pool.h
 *
 * Purpose: thead pool API define and implmenetation 
 *
 * Developer:
 *   wen.gu , 2023-11-03
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_CORE_XTHREAD_POOL_H__
#define __XBUS_CORE_XTHREAD_POOL_H__

#include <string>
#include <memory>
#include <functional>
#include <future>

namespace xbus::core {

class XThreadPool {
public:
    using TaskRunnable = std::function<void()>;
    
public:
    /**
     * stack_size: 0: Use default thread stack size 
     */
    XThreadPool(uint32_t min_capcity = 2, 
                uint32_t max_capcity = 16,
                uint32_t idle_time_sec = 40,
                uint32_t stack_size = 0);
    
    XThreadPool(const std::string& name,
                uint32_t min_capcity = 2, 
                uint32_t max_capcity = 16,
                uint32_t idle_time_sec = 40,
                uint32_t stack_size = 0);    
    virtual ~XThreadPool();

public:
    virtual uint32_t capcity() const { return max_capcity_; } /**< Returns the maximum capacity of threads. */
    virtual uint32_t used() const{ return used_workers_; }    /**< Returns the number of currently used threads. */
	virtual uint32_t allocated() const{ return allocated_workers_; } /**< Returns the number of currently allocated threads.*/
	virtual uint32_t available() const{ return available_workers_; } /**< Returns the number available threads.*/
    virtual int32_t priority() const { return priority_;}    
    virtual const std::string& name() const { return name_; }		
public:
    /**priority == 0 : use default thread priority */
    virtual bool start(int32_t priority = 0);
    virtual bool stop();
    virtual bool is_started();

public:
    bool addTask(TaskRunnable task);

    template<typename F, typename... Args>
    auto addTask(F&& func, Args&&... args) -> std::future<decltype(func(args...))> {
        // 获取函数返回值类型        
        using return_type = decltype(func(args...));
    
        // 创建一个指向任务的智能指针
        auto task = std::make_shared<std::packaged_task<return_type()>>(
                std::bind(std::forward<F>(func), std::forward<Args>(args)...)
            );
            
        std::future<return_type> res = task->get_future();

        addTask([task](){ (*task)(); });

        return res;       
    }

private:
    class Impl;
    int32_t priority_;
    uint32_t min_capcity_;
    uint32_t max_capcity_;
    uint32_t used_workers_;
    uint32_t available_workers_;
    uint32_t allocated_workers_;
    std::unique_ptr<Impl> impl_;
    std::string name_;

};

} //namespace xbus::core


#endif /** !__XBUS_CORE_XTHREAD_POOL_H__ */